﻿using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using GrainManage.Model;
using System;
using System.Linq;

namespace GrainManage.Tools
{
    public sealed class LoginHelper
    {
        private readonly string cmdGetAll = "SELECT UserName,Password,IsRemember FROM LoginHistory";
        private readonly string cmdGetID = "SELECT ID FROM LoginHistory WHERE UserName=?";
        private readonly string cmdInsert = "INSERT INTO LoginHistory(UserName,Password,IsRemember) VALUES(?,?,?)";
        private readonly string cmdUpdate = "UPDATE LoginHistory SET Password=?,IsRemember=? WHERE UserName=?";
        private readonly string cmdDelete = "DELETE FROM LoginHistory WHERE UserName=?";

        private readonly List<LoginHistory> historyList = new List<LoginHistory>();
        public List<LoginHistory> Users { get { return historyList; } }

        public void Add(string userName, string password, bool remember)
        {
            var encry = new GrainManage.Encrypt.EncryptionInfo();
            var encriedPassword = encry.EncryptString(password);
            if (SQLite.ExecuteScalar(cmdGetID, userName) == null)
            {
                SQLite.Execute(cmdInsert, userName, encriedPassword, remember);
            }
            else
            {
                SQLite.Execute(cmdUpdate, encriedPassword, remember, userName);
            }
        }

        public void Remove(string userName)
        {
            var affected = SQLite.Execute(cmdDelete, userName);
            if (affected > 0)
            {
                foreach (var user in historyList)
                {
                    if (user.UserName == userName)
                    {
                        historyList.Remove(user);
                        break;
                    }
                }
            }
        }

        public void LoadAll()
        {
            using (var reader = SQLite.ExecuteReader(cmdGetAll))
            {
                if (reader.HasRows)
                {
                    if (historyList.Count > 0)
                    {
                        historyList.Clear();
                    }
                    var encry = new GrainManage.Encrypt.EncryptionInfo();
                    while (reader.Read())
                    {
                        var login = new LoginHistory();
                        login.UserName = Convert.ToString(reader[0]);
                        login.IsRemember = Convert.ToBoolean(reader[2]);
                        if (login.IsRemember)
                        {
                            var encriedPassword = Convert.ToString(reader[1]);
                            var decriedPassword = encry.DecryptString(encriedPassword);
                            login.Password = decriedPassword;
                        }
                        historyList.Add(login);
                    }
                }
            }
        }
    }
}
